package com.stbu.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stbu.bean.Employments;
import com.stbu.models.EmploymentModel;
import com.stbu.server.EmploymentService;
import com.stbu.util.RestResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/employment")
@CrossOrigin(origins = "*")
public class EmploymentController {
    //自动注入
    @Autowired
    private EmploymentService employmentService;
    @RequestMapping("/getLists")
    public RestResponse<Page<Employments>> getAllEmployment(EmploymentModel param) {
        Page<Employments> employmentsPage = employmentService.getLists(param);
        return RestResponse.ok(employmentsPage);

    }

    @RequestMapping("/getEmpById")
    public RestResponse<Employments> getEmpById(String id) {
//        List<Employments> alist = new ArrayList<>();
        if (id == null || id.equals("")) {
            return RestResponse.fail("Id 不能为空");
        } else {
            Employments empOne = employmentService.getById(id);
//            alist.add(empOne);
            return RestResponse.ok(empOne);
        }
    }

    @RequestMapping("/deleteById")
    public RestResponse delEmploymentById(Integer id) {
        if (id == null) {
            return RestResponse.fail("id 无效");
        }
        boolean del = employmentService.removeById(id);
        if(del) {
            return RestResponse.ok();
        } else {
            return RestResponse.fail();
        }
    }

    @RequestMapping("/add")
    public RestResponse addEmployment(EmploymentModel param) {
            String addResult = employmentService.addEmployment(param);
            if (addResult !=null) {
                return RestResponse.fail(addResult);
            }
            return RestResponse.ok();

    }

    @RequestMapping("/update")
    public RestResponse updateEmployment(EmploymentModel param) {
        String upStr = employmentService.updateEmployment(param);
        if(upStr !=null) {
            return RestResponse.fail(upStr);
        }
        return RestResponse.ok();
    }

}
